﻿
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OF.Session;
namespace OF.DAL
{
	/// <summary>
	/// 数据访问类:OrderDetail
	/// </summary>
    public partial class OrderDetail : Session.DALBase
	{
		public OrderDetail()
		{}
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "OrderDetail");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from OrderDetail");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(OF.Model.OrderDetail model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into OrderDetail(");
            strSql.Append("ordersn,buyerid,dishid,count,price,allprice,orderid,dishname,sellerid)");
            strSql.Append(" values (");
            strSql.Append("@ordersn,@buyerid,@dishid,@count,@price,@allprice,@orderid,@dishname,@sellerid)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ordersn", SqlDbType.Int,4),
					new SqlParameter("@buyerid", SqlDbType.Int,4),
					new SqlParameter("@dishid", SqlDbType.Int,4),
					new SqlParameter("@count", SqlDbType.Int,4),
					new SqlParameter("@price", SqlDbType.Decimal,9),
					new SqlParameter("@allprice", SqlDbType.Decimal,9),
					new SqlParameter("@orderid", SqlDbType.Int,4),
					new SqlParameter("@dishname", SqlDbType.NVarChar,20),
					new SqlParameter("@sellerid", SqlDbType.Int,4)};
            parameters[0].Value = model.ordersn;
            parameters[1].Value = model.buyerid;
            parameters[2].Value = model.dishid;
            parameters[3].Value = model.count;
            parameters[4].Value = model.price;
            parameters[5].Value = model.allprice;
            parameters[6].Value = model.orderid;
            parameters[7].Value = model.dishname;
            parameters[8].Value = model.sellerid;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(OF.Model.OrderDetail model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update OrderDetail set ");
            strSql.Append("ordersn=@ordersn,");
            strSql.Append("buyerid=@buyerid,");
            strSql.Append("dishid=@dishid,");
            strSql.Append("count=@count,");
            strSql.Append("price=@price,");
            strSql.Append("allprice=@allprice,");
            strSql.Append("orderid=@orderid,");
            strSql.Append("dishname=@dishname,");
            strSql.Append("sellerid=@sellerid");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@ordersn", SqlDbType.Int,4),
					new SqlParameter("@buyerid", SqlDbType.Int,4),
					new SqlParameter("@dishid", SqlDbType.Int,4),
					new SqlParameter("@count", SqlDbType.Int,4),
					new SqlParameter("@price", SqlDbType.Decimal,9),
					new SqlParameter("@allprice", SqlDbType.Decimal,9),
					new SqlParameter("@orderid", SqlDbType.Int,4),
					new SqlParameter("@dishname", SqlDbType.NVarChar,20),
					new SqlParameter("@sellerid", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.ordersn;
            parameters[1].Value = model.buyerid;
            parameters[2].Value = model.dishid;
            parameters[3].Value = model.count;
            parameters[4].Value = model.price;
            parameters[5].Value = model.allprice;
            parameters[6].Value = model.orderid;
            parameters[7].Value = model.dishname;
            parameters[8].Value = model.sellerid;
            parameters[9].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from OrderDetail ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from OrderDetail ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OF.Model.OrderDetail GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,ordersn,buyerid,dishid,count,price,allprice,orderid,dishname,sellerid from OrderDetail ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            OF.Model.OrderDetail model = new OF.Model.OrderDetail();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OF.Model.OrderDetail DataRowToModel(DataRow row)
        {
            OF.Model.OrderDetail model = new OF.Model.OrderDetail();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["ordersn"] != null && row["ordersn"].ToString() != "")
                {
                    model.ordersn = int.Parse(row["ordersn"].ToString());
                }
                if (row["buyerid"] != null && row["buyerid"].ToString() != "")
                {
                    model.buyerid = int.Parse(row["buyerid"].ToString());
                }
                if (row["dishid"] != null && row["dishid"].ToString() != "")
                {
                    model.dishid = int.Parse(row["dishid"].ToString());
                }
                if (row["count"] != null && row["count"].ToString() != "")
                {
                    model.count = int.Parse(row["count"].ToString());
                }
                if (row["price"] != null && row["price"].ToString() != "")
                {
                    model.price = decimal.Parse(row["price"].ToString());
                }
                if (row["allprice"] != null && row["allprice"].ToString() != "")
                {
                    model.allprice = decimal.Parse(row["allprice"].ToString());
                }
                if (row["orderid"] != null && row["orderid"].ToString() != "")
                {
                    model.orderid = int.Parse(row["orderid"].ToString());
                }
                if (row["dishname"] != null)
                {
                    model.dishname = row["dishname"].ToString();
                }
                if (row["sellerid"] != null && row["sellerid"].ToString() != "")
                {
                    model.sellerid = int.Parse(row["sellerid"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,ordersn,buyerid,dishid,count,price,allprice,orderid,dishname,sellerid ");
            strSql.Append(" FROM OrderDetail ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,ordersn,buyerid,dishid,count,price,allprice,orderid,dishname,sellerid ");
            strSql.Append(" FROM OrderDetail ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM OrderDetail ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from OrderDetail T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "OrderDetail";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetOrderDishList(string orderid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.id,a.dishid,a.count,a.price,a.allprice,b.dishname FROM OrderDetail a");
            strSql.Append(" left join Dish b on a.dishid =b.id ");
            strSql.Append(" where orderid=" + orderid); 
            return DbHelperSQL.Query(strSql.ToString());
        }
        public bool DeleteByOrderid(int orderid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from OrderDetail ");
            strSql.Append(" where orderid="+orderid);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		#endregion  ExtensionMethod
	}
}

